<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/8/3
 * Time: 15:43
 */
namespace app\admin\controller;
use app\common\model\AdModel;
use app\common\model\BannerModel;
use think\Loader;

class Banner extends Base{
    public function index(){
        //查询所有的banner
        $data = BannerModel::order('type','desc')->order('sort','desc')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    //添加
    public function add(){

        if(request()->isPost()){
            //接受参数
            $data = input();
            //启用验证器
            $bannervild = Loader::validate('BannerValidate');
            if(!$bannervild->check($data)) return $this->error($bannervild->getError);
            //记录数据库
            $bannermodel = new BannerModel();
            $re = $bannermodel->allowField(true)->save($data);
            if($re){
                $this->redirect('/admin/banner/index');
            }else{
                $this->error('添加失败');
            }
        }else{
            return $this->fetch('add');
        }
    }
    //编辑
    public function edit(){
        if(request()->isPost()){
            //接受参数
            $data = input();
            if(!$data) return $this->error('参数错误');
            //验证
            $bannervild = Loader::validate('BannerValidate');
            if(!$bannervild->check($data)) return $this->error($bannervild->getError);
            //查看图片是否重新上传
            $bannermodel  = BannerModel::get($data['id']);
            $re = $bannermodel->allowField(true)->save($data);
            if($re){
                if($data['src'] != $data['old_src']){

                    if(file_exists(ROOT_PATH.$data['old_src'])){
                        unlink(ROOT_PATH.$data['old_src']);
                    }
                }
                return $this->redirect('/admin/banner/index');
            }else{
                return $this->error('修改失败');
            }

        }else{
            $id = input('id');
            if(!$id) return $this->error('参数错误');
            $data = BannerModel::get($id);
            if(!$data) return $this->error('该数据不存在');
            $this->assign('data',$data);
            return $this->fetch();
        }
    }
    //删除
    public function del(){
        if(request()->isAjax()){
            if(!input('id')) return json(['code'=>2,'data'=>'','msg'=>'参数错误']);
            $ee = BannerModel::get(input('id'));
            if($ee){
                if(file_exists(ROOT_PATH.$ee['src'])){
                    unlink(ROOT_PATH.$ee['src']);
                }
            }

            $re = BannerModel::destroy(input('id'));
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'删除成功']);
            }else{
                return json(['code'=>1,'data'=>'','msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }


}